/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vista;

import java.awt.event.ItemEvent;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
/**
 *
 * @author rfernan
 */
public class Personal extends javax.swing.JFrame {
    /**
     * Creates new form Personal
     */
    private Controlador.Personal personalController;
    
    public Personal() {
        initComponents();
        setSize(400,400);
        setResizable(false);
        setLocationRelativeTo(null);
        personalController = new Controlador.Personal();
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        List llenado = new ArrayList();
        jComboBoxEmpleado.removeAllItems();
        jComboBoxEmpleado.addItem(" ");
        llenado = personalController.cargarEmpleadosBox();
        
        for(int i = 0; i < llenado.size(); i++){
            this.jComboBoxEmpleado.addItem(llenado.get(i));
        }
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Jpanel4 = new javax.swing.JPanel();
        modificarLabel = new javax.swing.JLabel();
        nombreLabel = new javax.swing.JLabel();
        nombreField = new javax.swing.JTextField();
        descripcionField = new javax.swing.JTextField();
        guardarButton = new javax.swing.JButton();
        eliminarButton = new javax.swing.JButton();
        nuevoButton = new javax.swing.JButton();
        nombreField1 = new javax.swing.JTextField();
        nombreLabel1 = new javax.swing.JLabel();
        jComboBoxEmpleado = new javax.swing.JComboBox();
        passwordLabel = new javax.swing.JLabel();
        rutTextField = new javax.swing.JTextField();
        fonoTextField = new javax.swing.JTextField();
        rolTextField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(352, 326));

        Jpanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Personal"));

        modificarLabel.setText("Empleado");

        nombreLabel.setText("Nombre:");

        guardarButton.setText("Guardar");
        guardarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardarButtonActionPerformed(evt);
            }
        });

        eliminarButton.setText("Eliminar");
        eliminarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eliminarButtonActionPerformed(evt);
            }
        });

        nuevoButton.setText("Nuevo");
        nuevoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nuevoButtonActionPerformed(evt);
            }
        });

        nombreLabel1.setText("Usuario:");

        jComboBoxEmpleado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { " " }));
        jComboBoxEmpleado.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBoxEmpleadoItemStateChanged(evt);
            }
        });

        passwordLabel.setText("Password:");

        jLabel1.setText("Rut:");

        jLabel2.setText("Telefono:");

        jLabel3.setText("Rol:");

        javax.swing.GroupLayout Jpanel4Layout = new javax.swing.GroupLayout(Jpanel4);
        Jpanel4.setLayout(Jpanel4Layout);
        Jpanel4Layout.setHorizontalGroup(
            Jpanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(Jpanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(Jpanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(Jpanel4Layout.createSequentialGroup()
                        .addComponent(modificarLabel)
                        .addGap(18, 18, 18)
                        .addComponent(jComboBoxEmpleado, 0, 236, Short.MAX_VALUE))
                    .addGroup(Jpanel4Layout.createSequentialGroup()
                        .addComponent(nuevoButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(guardarButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(eliminarButton))
                    .addGroup(Jpanel4Layout.createSequentialGroup()
                        .addGroup(Jpanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nombreLabel)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(22, 22, 22)
                        .addGroup(Jpanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(nombreField, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
                            .addComponent(rutTextField)
                            .addComponent(fonoTextField)))
                    .addGroup(Jpanel4Layout.createSequentialGroup()
                        .addGroup(Jpanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nombreLabel1)
                            .addComponent(jLabel3))
                        .addGap(28, 28, 28)
                        .addGroup(Jpanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nombreField1)
                            .addComponent(rolTextField)))
                    .addGroup(Jpanel4Layout.createSequentialGroup()
                        .addComponent(passwordLabel)
                        .addGap(18, 18, 18)
                        .addComponent(descripcionField)))
                .addContainerGap(99, Short.MAX_VALUE))
        );
        Jpanel4Layout.setVerticalGroup(
            Jpanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(Jpanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(Jpanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(modificarLabel)
                    .addComponent(jComboBoxEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(Jpanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nombreField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nombreLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(Jpanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rutTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(Jpanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fonoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 64, Short.MAX_VALUE)
                .addGroup(Jpanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rolTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(Jpanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nombreField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nombreLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(Jpanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passwordLabel)
                    .addComponent(descripcionField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(Jpanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE, false)
                    .addComponent(nuevoButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(guardarButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(eliminarButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(26, 26, 26))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(Jpanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(Jpanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void guardarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarButtonActionPerformed
        
        this.personalController = new Controlador.Personal();
        valida = personalController.valida(nombreField1.getText());
                
        if(valida == true){
            
            JOptionPane.showMessageDialog(null,"El usuario "+nombreField1.getText()+" ya existe.", "ERROR", JOptionPane.ERROR_MESSAGE);
        }
        else{
            personalController.ingresa(nombreField.getText(),rutTextField.getText(),fonoTextField.getText(),rolTextField.getText(),nombreField1.getText(),descripcionField.getText());
            JOptionPane.showMessageDialog(null,"El usuario "+nombreField1.getText()+" fue creado exitosamente.", "Confirmacion de creacion", JOptionPane.PLAIN_MESSAGE);
            List llenado = new ArrayList();
            jComboBoxEmpleado.removeAllItems();
            jComboBoxEmpleado.addItem(" ");
            llenado = personalController.cargarEmpleadosBox();
        
            for(int i = 0; i < llenado.size(); i++){
                this.jComboBoxEmpleado.addItem(llenado.get(i));
            }
        }
        
    }//GEN-LAST:event_guardarButtonActionPerformed

    private void nuevoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nuevoButtonActionPerformed
        jComboBoxEmpleado.setSelectedIndex(0);
        nombreField.setText("");
        nombreField1.setText("");
        rutTextField.setText("");
        fonoTextField.setText("");
        rolTextField.setText("");
        descripcionField.setText("");
    }//GEN-LAST:event_nuevoButtonActionPerformed

    private void eliminarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eliminarButtonActionPerformed
        this.personalController = new Controlador.Personal();
        int i =JOptionPane.showConfirmDialog(this,"¿Realmente Desea Borrar el Usuario? "+nombreField1.getText(),"Confirmar Eliminacion",JOptionPane.YES_NO_OPTION);
        if(i==0){
            personalController.borra(nombreField.getText(),rutTextField.getText(),fonoTextField.getText(),rolTextField.getText(),nombreField1.getText(),descripcionField.getText());
            JOptionPane.showMessageDialog(null,"El usuario "+nombreField1.getText()+" fue eliminado exitosamente.", "Confirmacion de eliminacion", JOptionPane.PLAIN_MESSAGE);
            List llenado = new ArrayList();
            jComboBoxEmpleado.removeAllItems();
            jComboBoxEmpleado.addItem(" ");
            llenado = personalController.cargarEmpleadosBox();
        
            for(int a = 0; a < llenado.size(); a++){
                this.jComboBoxEmpleado.addItem(llenado.get(a));
            }
            
            jComboBoxEmpleado.setSelectedIndex(0);
            nombreField.setText("");
            nombreField1.setText("");
            rutTextField.setText("");
            fonoTextField.setText("");
            rolTextField.setText("");
            descripcionField.setText("");
        }
    }//GEN-LAST:event_eliminarButtonActionPerformed

    private void jComboBoxEmpleadoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBoxEmpleadoItemStateChanged
       if(jComboBoxEmpleado.getSelectedIndex() > 0){
            this.personalController = new Controlador.Personal();
            String seleccion = jComboBoxEmpleado.getSelectedItem().toString();
            nombreField.setText(personalController.nombreEmpleado(seleccion));
            nombreField1.setText(seleccion);
            descripcionField.setText(personalController.contrasenaEmpleado(seleccion));
            rutTextField.setText(personalController.rutEmpleado(seleccion));
            fonoTextField.setText(personalController.telefonoEmpleado(seleccion));
            rolTextField.setText(personalController.rolEmpleado(seleccion));
       }
    }//GEN-LAST:event_jComboBoxEmpleadoItemStateChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Personal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Personal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Personal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Personal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Personal().setVisible(true);
            }
        });
    }
    private boolean valida;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel Jpanel4;
    private javax.swing.JTextField descripcionField;
    private javax.swing.JButton eliminarButton;
    private javax.swing.JTextField fonoTextField;
    private javax.swing.JButton guardarButton;
    private javax.swing.JComboBox jComboBoxEmpleado;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel modificarLabel;
    private javax.swing.JTextField nombreField;
    private javax.swing.JTextField nombreField1;
    private javax.swing.JLabel nombreLabel;
    private javax.swing.JLabel nombreLabel1;
    private javax.swing.JButton nuevoButton;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JTextField rolTextField;
    private javax.swing.JTextField rutTextField;
    // End of variables declaration//GEN-END:variables
}
